﻿using Nancy;
using Nancy.Extensions;

namespace BLWCMS.Web.Modules
{
    public class BaseModule : NancyModule
    {
        public BaseModule()
        {

        }

        public BaseModule(string modulePath)
            : base(modulePath)
        {

        }

        public BaseModule(string modulePath,bool isLogin)
            : base(modulePath)
        {
            if (isLogin)
            {
                Before += GoToLogin;
            }
            Before += GetUserInfo;
        }

        private Response GoToLogin(NancyContext context)
        {
            if (Session["loginedUser"] == null)
            {
                return context.GetRedirect("/login?returnUrl=" + Request.Url.Path);
            }
            else
            {
                Session["loginedUser"] = Session["loginedUser"];
                return null;
            }
        }

        private Response GetUserInfo(NancyContext context)
        {
            ViewBag.UserId = ((string[]) Session["loginedUser"])[0];
            ViewBag.UserNick = ((string[]) Session["loginedUser"])[1];
            return null;
        }

    }
}